package cn.kgc.shiro.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author 课工场
 * @date 2024/12/5
 * @description  用户自定义realm  实现根据数据库完成认证和授权操作
 */
public class CustomerRealm extends AuthorizingRealm {


    //  授权方法   授权器自动调用
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }


    //认证方法    认证器自动调用    根据用户名查询用户信息
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("----CustomerRealm  doGetAuthenticationInfo----");

        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        String username = usernamePasswordToken.getUsername();
        System.out.println("username = " + username);
        // TODO 后续实际用用中  根据用户名 查询数据库   user     root   123456   mybatis
        if (username.equals("root")){   //   user   ->  AuthenticationInfo
            SimpleAccount simpleAccount = new SimpleAccount("root", "e10adc3949ba59abbe56e057f20f883e", this.getName());
            return  simpleAccount;
        }

        return null;
    }
}
